package cn.buk.tms.entity.order.flight;

import cn.buk.common.util.DecimalUtil;
import cn.buk.tms.entity.base.BaseOrderInfo;
import com.fasterxml.jackson.annotation.JsonFormat;

import jakarta.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 机票退票订单
 *
 * @author yfdai
 */
@Entity
@Table(name = "flt_refund_order",
        uniqueConstraints = @UniqueConstraint(name = "uk_flt_refund_order", columnNames = {"enterprise_id", "balance_code", "ticket_no"}))
@AssociationOverride(name = "customer",
        joinColumns = @JoinColumn(name = "customer_id"),
        foreignKey = @ForeignKey(name = "fk_flt_refund_customer")
)
@AssociationOverride(name = "supplier",
        joinColumns = @JoinColumn(name = "supplier_id"),
        foreignKey = @ForeignKey(name = "fk_flt_refund_supplier")
)
@AssociationOverride(name = "paymentMethod",
        joinColumns = @JoinColumn(name = "payment_method_id"),
        foreignKey = @ForeignKey(name = "fk_flt_refund_payment_method")
)
public class FlightRefundOrder extends BaseOrderInfo {
  /**
   * 0 - 提交退票，等待处理
   * 1 - 我来处理
   * 2 - 已向航司提交退票
   * 4 - 取消了
   * 状态2之后可以变为状态8 或 16
   * 8 - 航司已退，待退客人
   * 16 - 已退客人
   * 状态8 和状态16都达到了，就可以到32状态了
   * 32 - 已完成
   */
  public static final int ORDER_STATUS_WAITING = 0;
  public static final int ORDER_STATUS_WORKING = 1;
  public static final int ORDER_STATUS_SUBMITTED = 2;
  public static final int ORDER_STATUS_CANCELED = 4;
  public static final int ORDER_STATUS_AIR_REFUNDED = 8;
  public static final int ORDER_STATUS_PASSENGER_REFUNDED = 16;
  public static final int ORDER_STATUS_DONE = 32;


  @Column(name = "balance_code", length = 3)
  private String balanceCode;

  /**
   * 要退的机票的票号
   */
  @Column(name = "ticket_no", length = 10)
  private String ticketNo;

  /**
   * 退票单号
   */
  @Column(name = "refund_order_no", length = 10)
  private String refundOrderNo;

  /**
   * CZ NDC外部退票单号
   */
  @Column(name = "external_order_no", length = 20)
  private String externalOrderNo;

  /**
   * 机票订单号
   * 订单号(该退票来自哪个订单）
   * 原始单号
   */
  @Column(name = "order_no", length = 10)
  private String orderNo;

  /**
   * 关联的机票订单id
   */
  @Column(name = "order_id")
  private Integer orderId;


  /**
   * 旅客姓名
   */
  @Column(name = "psg_name", length = 50)
  private String psgName;

  @Column(name = "id_no", length = 20)
  private String idNo;

  /**
   * 联系电话, 乘机人的联系电话，和机票订单中乘机人的联系电话相同
   */
  @Column(length = 20)
  private String mobile;


  /**
   * 退票理由代码
   * 0-自愿退票
   * 1-非自愿退票
   * 4-废票
   */
  @Column(name = "reason_code")
  private int reasonCode;

  /**
   * 退票理由
   */
  @Column(name = "reason_desc")
  private String reasonDesc;

  private Double parvalue;

  private Double tax;

  /**
   * 原付票款额
   * 收客时客户支付的实际金额
   */
  @Column(name = "ticket_amount")
  private double ticketAmount;

  /**
   * 收客时此票的成本
   */
  private Double cost;

  /**
   * 企业收的退票服务费
   */
  @Column(name = "service_charge")
  private double serviceCharge;

  /**
   * 航司退票费
   */
  @Column(name = "air_refund_fee")
  private double airRefundCharge;

  /**
   * 预计航司实际退款金额（或同行退）
   */
  @Column(name = "airline_refund_amount")
  private Double airlineRefundAmount;

  /**
   * 航司实际退款金额
   */
  @Column(name = "airline_refund_amount_1")
  private Double airlineRefundAmountFinal;

  /**
   * 实际退给客人的金额
   */
  @Column(name = "psg_refund_amount")
  private Double passengerRefundAmount;

  private Double profit;
  /**
   * 0 - 提交退票，等待处理
   * 1 - 我来处理
   * 2 - 已向航司提交退票
   * 状态2之后可以变为状态8 或 16
   * 8 - 航司已退，待退客人
   * 16 - 已退客人
   * 状态8 和状态16都达到了，就可以到32状态了
   * 32 - 已完成
   */
  private int status;

  /**
   * 航司退款状态
   * 0、null 未退
   * 1 已退
   */
  @Column(name = "air_refund_status")
  private Integer airRefundStatus;

  /**
   * 2 - 已销账
   */
  @Column(name = "pay_status")
  private Integer payStatus;


  private String remark;

  @OneToMany(mappedBy = "order", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
  @OrderBy
  private Set<FlightRefundOrderDetail> flights;

  /**
   * 是否国际票退票
   * 1-是
   */
  @Column(name = "intl_ticket")
  private Integer intlTicket;


  /**
   * 提交退票的日期
   */
  @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
  @Temporal(TemporalType.DATE)
  @Column(name = "submit_date")
  private Date submitDate;

  /**
   * 航司退票日期
   */
  @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
  @Temporal(TemporalType.DATE)
  @Column(name = "air_refund_date")
  private Date airlineRefundDate;

  /**
   * 实退客户日期
   */
  @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
  @Temporal(TemporalType.DATE)
  @Column(name = "psg_refund_date")
  private Date psgRefundDate;


  @Column(name = "pnr_no", length = 10)
  private String pnrNo;

  /**
   * 联系人（代订人）(从机票订单过来）冗余
   */
  @Column(length = 20)
  private String linkman;

  /**
   * 联系电话（代订人）(从机票订单过来）冗余
   */
  @Column(length = 20, name = "link_phone")
  private String linkPhone;


  /**
   * DETR刷新状态
   * 1 - 已刷新
   * 2 - 已更新到FlightOrderTicket中
   * 4 - 找不到，应该是获取不到DETR信息，非授权OFFICE开票
   * 8 - 票号状态已到终态，无需再检查
   */
  @Column(name = "detr_refresh_status")
  private Integer detrRefreshStatus;


  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "detr_refresh_time")
  private Date detrRefreshTime;


  public String getRemark() {
    return remark;
  }

  public void setRemark(String remark) {
    this.remark = remark;
  }

  public int getStatus() {
    return status;
  }

  public void setStatus(int status) {
    this.status = status;
  }


  public String getTicketNo() {
    return ticketNo;
  }

  public void setTicketNo(String ticketNo) {
    this.ticketNo = ticketNo;
  }

  public String getPsgName() {
    return psgName;
  }

  public void setPsgName(String psgName) {
    this.psgName = psgName;
  }


  public String getBalanceCode() {
    return balanceCode;
  }

  public void setBalanceCode(String balanceCode) {
    this.balanceCode = balanceCode;
  }

  public double getTicketAmount() {
    return ticketAmount;
  }

  public void setTicketAmount(double ticketAmount) {
    this.ticketAmount = ticketAmount;
  }

  public double getServiceCharge() {
    return serviceCharge;
  }

  public void setServiceCharge(double serviceCharge) {
    this.serviceCharge = serviceCharge;
  }

  public double getAirRefundCharge() {
    return airRefundCharge;
  }

  public void setAirRefundCharge(double airRefundCharge) {
    this.airRefundCharge = airRefundCharge;
  }


  public Double getAirlineRefundAmount() {
    return airlineRefundAmount;
  }

  public void setAirlineRefundAmount(double airlineRefundAmount) {
    this.airlineRefundAmount = airlineRefundAmount;
  }

  public Double getPassengerRefundAmount() {
    return passengerRefundAmount;
  }

  public void setPassengerRefundAmount(double passengerRefundAmount) {
    this.passengerRefundAmount = passengerRefundAmount;
  }

  public String getOrderNo() {
    return orderNo;
  }

  public void setOrderNo(String orderNo) {
    this.orderNo = orderNo;
  }

  public String getReasonDesc() {
    return reasonDesc;
  }

  public void setReasonDesc(String reasonDesc) {
    this.reasonDesc = reasonDesc;
  }

  public int getReasonCode() {
    return reasonCode;
  }

  public void setReasonCode(int reasonCode) {
    this.reasonCode = reasonCode;
  }


  public Integer getOrderId() {
    return orderId == null ? 0 : orderId;
  }

  public void setOrderId(Integer orderId) {
    this.orderId = orderId;
  }

  public Set<FlightRefundOrderDetail> getFlights() {
    if (flights == null) {
      flights = new HashSet<>();
    }
    return flights;
  }

  public void setFlights(Set<FlightRefundOrderDetail> flights) {
    this.flights = flights;
  }


  public String getIdNo() {
    return idNo;
  }

  public void setIdNo(String idNo) {
    this.idNo = idNo;
  }

  public String getRefundOrderNo() {
    return refundOrderNo;
  }

  public void setRefundOrderNo(String refundOrderNo) {
    this.refundOrderNo = refundOrderNo;
  }


  public Double getProfit() {
    if (profit == null || profit == 0) {
      double airRefundAmount = getAirlineRefundAmount() == null ? 0 : getAirlineRefundAmount();
      double psgRefundAmount = getPassengerRefundAmount() == null ? 0 : getPassengerRefundAmount();

      profit = airRefundAmount - psgRefundAmount;
      profit = DecimalUtil.keepScale2(profit);
    }
    return profit;
  }

  public void setProfit(Double profit) {
    this.profit = profit;
  }

  public void setIntlTicket(int intlTicket) {
    this.intlTicket = intlTicket;
  }

  public int getIntlTicket() {
    return intlTicket == null ? 0 : intlTicket;
  }

  public Double getParvalue() {
    return parvalue == null ? 0 : parvalue;
  }

  public void setParvalue(double parvalue) {
    this.parvalue = parvalue;
  }

  public Double getTax() {
    return tax == null ? 0 : tax;
  }

  public void setTax(double tax) {
    this.tax = tax;
  }

  public Double getCost() {
    return cost == null ? 0 : cost;
  }

  public void setCost(double cost) {
    this.cost = cost;
  }


  public Date getSubmitDate() {
    return submitDate;
  }

  public void setSubmitDate(Date submitDate) {
    this.submitDate = submitDate;
  }

  public Date getAirlineRefundDate() {
    return airlineRefundDate;
  }

  public void setAirlineRefundDate(Date airlineRefundDate) {
    this.airlineRefundDate = airlineRefundDate;
  }

  public Date getPsgRefundDate() {
    return psgRefundDate;
  }

  public void setPsgRefundDate(Date psgRefundDate) {
    this.psgRefundDate = psgRefundDate;
  }


  public String getPnrNo() {
    return pnrNo;
  }

  public void setPnrNo(String pnrNo) {
    this.pnrNo = pnrNo;
  }

  public String getLinkman() {
    return linkman;
  }

  public void setLinkman(String linkman) {
    this.linkman = linkman;
  }

  public String getLinkPhone() {
    return linkPhone;
  }

  public void setLinkPhone(String linkPhone) {
    this.linkPhone = linkPhone;
  }


  public Integer getPayStatus() {
    return payStatus == null ? 0 : payStatus;
  }

  public void setPayStatus(Integer payStatus) {
    this.payStatus = payStatus;
  }

  public Double getAirlineRefundAmountFinal() {
    return airlineRefundAmountFinal;
  }

  public void setAirlineRefundAmountFinal(Double airlineRefundAmountFinal) {
    this.airlineRefundAmountFinal = airlineRefundAmountFinal;
  }

  public int getAirRefundStatus() {
    return airRefundStatus == null ? 0 : airRefundStatus;
  }

  public void setAirRefundStatus(Integer airRefundStatus) {
    this.airRefundStatus = airRefundStatus;
  }

  public String getMobile() {
    return mobile == null ? "" : mobile.trim();
  }

  public void setMobile(String mobile) {
    this.mobile = mobile;
  }


  public Integer getDetrRefreshStatus() {
    return detrRefreshStatus;
  }

  public void setDetrRefreshStatus(Integer detrRefreshStatus) {
    this.detrRefreshStatus = detrRefreshStatus;
  }

  public Date getDetrRefreshTime() {
    return detrRefreshTime;
  }

  public void setDetrRefreshTime(Date detrRefreshTime) {
    this.detrRefreshTime = detrRefreshTime;
  }

  public String getExternalOrderNo() {
    return externalOrderNo;
  }

  public void setExternalOrderNo(String externalOrderNo) {
    this.externalOrderNo = externalOrderNo;
  }
}
